package com.jxw.system.mediaserver;

import android.util.Log;
import com.vanhon.engine.decrypt.DecryptStream;
import java.io.IOException;
import java.net.URI;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import net.lingala.zip4j.util.InternalZipConstants;
import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.RequestLine;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/* loaded from: classes.dex */
public class HttpServer {
    private static final String TAG = "HttpServer";
    private static final EncMediaRequestHandler mHttpEncMediaHandler = new EncMediaRequestHandler();
    private static final Map<String, HttpRequestHandler> mHttpHandler = new HashMap();
    private static final Object mHttpServerLock = new Object();
    private static HttpFileServer mHttpWtbServer;

    public static void createHttpServer() {
        synchronized (mHttpServerLock) {
            if (mHttpWtbServer != null) {
                return;
            }
            mHttpHandler.put("/EncMedia/*", mHttpEncMediaHandler);
            mHttpWtbServer = HttpFileServer.create(8100, mHttpHandler);
        }
    }

    public static void release() {
        mHttpWtbServer.exit();
    }

    /* loaded from: classes.dex */
    private static class EncMediaRequestHandler implements HttpRequestHandler {
        private static final boolean DEBUG = true;
        private static final String TAG = "FileRequestHandler";

        private EncMediaRequestHandler() {
        }

        @Override // org.apache.http.protocol.HttpRequestHandler
        public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
            String str;
            int i;
            dumpRequest(httpRequest);
            boolean containsHeader = httpRequest.containsHeader("Range");
            Header[] headers = httpRequest.getHeaders("Range");
            int i2 = 0;
            if (headers == null || headers.length <= 0) {
                str = "";
                containsHeader = false;
            } else {
                str = headers[0].getValue();
            }
            Map<String, String> parseQuery = parseQuery(httpRequest.getRequestLine());
            String str2 = parseQuery.get("schema");
            String str3 = null;
            String decode = str2 != null ? URLDecoder.decode(str2, "utf8") : null;
            String str4 = parseQuery.get(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL);
            String decode2 = str4 != null ? URLDecoder.decode(str4, "utf8") : null;
            if (decode != null && decode2 != null && (decode.compareToIgnoreCase("file") == 0 || decode.compareToIgnoreCase(HttpHost.DEFAULT_SCHEME_NAME) == 0)) {
                str3 = decode + "://" + decode2;
            }
            if (str3 != null) {
                DecryptStream create = DecryptStream.create(str3);
                if (create == null) {
                    httpResponse.setStatusCode(404);
                    return;
                }
                int length = create.getLength();
                int i3 = length - 1;
                if (containsHeader) {
                    int[] parseRange = parseRange(str);
                    if (parseRange != null) {
                        i2 = parseRange[0];
                        i = parseRange[1];
                    } else {
                        i = i3;
                    }
                    if (i >= 0 && i < length) {
                        i3 = i;
                    }
                    create.skip(i2);
                }
                BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
                basicHttpEntity.setContentType("application/octet-stream");
                basicHttpEntity.setContent(create);
                httpResponse.setEntity(basicHttpEntity);
                httpResponse.addHeader(HTTP.CONN_DIRECTIVE, "keep-alive");
                if (containsHeader) {
                    create.setLength(i3 + 1);
                    httpResponse.addHeader("Content-Range", "bytes " + i2 + "-" + i3 + InternalZipConstants.ZIP_FILE_SEPARATOR + Integer.toString(length));
                    httpResponse.setHeader(HTTP.CONTENT_LEN, Integer.toString((i3 - i2) + 1));
                    httpResponse.setStatusCode(206);
                } else {
                    httpResponse.setHeader(HTTP.CONTENT_LEN, Integer.toString(length));
                    httpResponse.setStatusCode(200);
                }
            } else {
                httpResponse.setStatusCode(403);
            }
            dumpResponse(httpResponse);
        }

        private Map<String, String> parseQuery(RequestLine requestLine) {
            HashMap hashMap = new HashMap();
            String[] split = URI.create(requestLine.getUri()).getQuery().split("&");
            if (split != null) {
                for (int i = 0; i < split.length; i++) {
                    int indexOf = split[i].indexOf("=");
                    if (indexOf > 0) {
                        hashMap.put(split[i].substring(0, indexOf), split[i].substring(indexOf + 1));
                    }
                }
            }
            return hashMap;
        }

        private int[] parseRange(String str) {
            String[] split;
            String trim = str.trim();
            if (trim == null || trim.length() < 8 || (split = trim.substring(6).split("-")) == null || split.length < 1) {
                return null;
            }
            return new int[]{Integer.parseInt(split[0]), split.length > 1 ? Integer.parseInt(split[1]) : -1};
        }

        private void dumpRequest(HttpRequest httpRequest) {
            Header[] allHeaders = httpRequest.getAllHeaders();
            Log.e(TAG, "dumpRequest, request line: " + httpRequest.getRequestLine() + ", version: " + httpRequest.getProtocolVersion());
            for (int i = 0; i < allHeaders.length; i++) {
                Log.e(TAG, "dumpRequest, header[" + i + "], name: " + allHeaders[i].getName() + ", value: " + allHeaders[i].getValue());
            }
            String query = URI.create(httpRequest.getRequestLine().getUri()).getQuery();
            Log.e(TAG, "dumpRequest, query: " + query);
        }

        private void dumpResponse(HttpResponse httpResponse) {
            Log.e(TAG, "dumpResponse, status: " + httpResponse.getStatusLine());
            Header[] allHeaders = httpResponse.getAllHeaders();
            for (int i = 0; i < allHeaders.length; i++) {
                Log.e(TAG, "dumpResponse, header[" + i + "], name: " + allHeaders[i].getName() + ", value: " + allHeaders[i].getValue());
            }
        }
    }
}
